const mongo = require('mongoose')
const fs = require('fs')
const conf = require('../conf/config') 
import {resolve} from 'path'

const models = resolve(__dirname,'../database')

fs.readdirSync(models)
.filter(file => ~file.search(/^[^\.].*js$/))
.forEach(file => require(resolve(models,file)))


export const database = app =>{
   
    // 连接数据库
    mongo.set('debug',true)

    mongo.connect(conf.dbURl)

    mongo.connection.on('disconnected',()=>{
        mongo.connect(conf.dbURl)
    })

    mongo.connection.on('error',(err)=>{
        console.error(err)
    })

    mongo.connection.on('open',async ()=>{
        console.log('打开数据库%s成功',conf.dbURl)
      //  require('../wechat/index')
    })



    
}